import Config from "./config";
import Util from "./util";
import Interfaces from "./interface";

const Game = (function () {
  // 获取各种节点
  let throttle,
    // 获取牌桌的dom节点
    $selector = Util.getElement("#selector"),
    // 按钮

    // 牌堆列表的dom节点
    $banker = Util.getElement(".banker"),
    $player = Util.getElement(".player"),
    // 获取点数显示区的dom节点
    $countB = Util.getElement(".countB"),
    $countP = Util.getElement(".countP"),
    // 获取余额显示区的dom节点
    $balance = Util.getElement("#balance"),
    $bankerBalance = Util.getElement("#bankerBalance"),
    // 获取遮罩层的dom节点
    $mask = Util.getElement(".mask"),
    $promot = Util.getElement(".promot");

  // 游戏初始化方法
  let init = function () {
    //游戏初始化操作 普通属性已在config.js

    //初始化事件绑定
    eventBind();
  };

  let eventBind = function () {
    // 处理函数,当牌桌区域被点击时触发
    $selector.addEventListener(
      "click",
      function (e) {
        // 如果点击的是开始按钮区域
        if (e.target.id == "begin") {
          //检查余额是否为0
          if (
            Interfaces.roleInfo.player.balance == 0 ||
            Interfaces.roleInfo.player.balance == 150
          ) {
            Interfaces.roleInfo.player.balance = 100;
            Interfaces.roleInfo.banker.balance = 50;
          }

          // 更新用户余额
          Util.updateBalance($balance, -10);
          // 更新庄家余额
          Util.updateBankerBalance($bankerBalance, 0);

          // 开始按钮和规则按钮隐藏，其它按钮出现
          Util.butState($selector);

          //正式发牌，每发一张牌计算对应点数并显示
          Interfaces.divCards(
            $banker,
            $player,
            $countB,
            $countP,
            $mask,
            $promot,
            $balance,
            $bankerBalance
          );
        } else if (e.target.id == "rules") {
          // 居中弹出规则框
          Interfaces.promotMes($mask, $promot, false, Config.rules);
        } else if (e.target.id == "addCard") {
          // 如果点击的是'要牌'按钮
          Interfaces.addCard(
            "Player",
            $player,
            $countP,
            $mask,
            $promot,
            $balance,
            $banker,
            $bankerBalance
          );
        } else if (e.target.id == "stopCard") {
          // 如果点击的是'停牌'按钮
          if (throttle) {
            clearTimeout(throttle);
            //有可能出现Interfaces.stopCard已经在执行了又点击了停牌的情况 或者禁用按钮也可
            if ($banker.lastElementChild.className != "unknow") {
              return;
            }
          }
          throttle = setTimeout(function () {
            Interfaces.stopCard(
              "Dealer",
              $banker,
              $countB,
              $mask,
              $promot,
              $balance,
              $banker,
              $bankerBalance
            );
          }, 200);
        }
      },
      false
    );

    //遮罩层
    $mask.addEventListener(
      "click",
      function (e) {
        if (e.target.id != "promot") {
          let state = $selector.dataset.show.indexOf("rules");
          if (state != -1) {
            Interfaces.promotMes($mask, $promot, true);
            return;
          } else {
            //从游戏页转来的
            Interfaces.promotMes($mask, $promot, true);

            //点击提示框外部后清除li和显示的点值
            Util.clearItem();
            //清除点数
            Util.updateRoleInfo($banker, $countB, "banker");
            Util.updateRoleInfo($player, $countP, "player");
            //按钮切换
            Util.butState($selector);
          }
        }
      },
      false
    );
  };

  return {
    init: init,
  };
})();

// 执行游戏初始化方法
Game.init()


